package com.ziyun.job.modules.job.task.order;

import com.baomidou.mybatisplus.plugins.Page;
import com.ziyun.erp.modules.order.entity.OrderInfoEntity;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author guizhao
 * @Description: 同步 紫云网订单
 *      紫云网订单状态改变
 *          1.生产订单
 *          2.订单支付
 *          3.确认收货
 *          4.取消订单
 * @date 2018-08-049:53
 */
@Component("synZiyunOrderTask")
public class SynZiyunOrderTask {

    /**
     * 日志
     */
    private  static  Logger logger = LoggerFactory.getLogger(SynZiyunOrderTask.class);

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private ErpOrderService erpOrderService;

    /**
     * 同步 紫云网新生产订单
     */
    public void synProductionOrder(){
        // 查询 3天内紫云网订单未同步订单数量
//        String startTime = DateUtils.format(DateUtils.getYesterdayTwoStart(),DateUtils.DATE_TIME_PATTERN);
        String startTime = "2018-01-01 00:00:00";
        logger.info("同步紫云网订单起始时间：" + startTime);

        // 当前页数
        int currentPage = 1;

        Page<OrderInfoEntity> page = new Page<>(currentPage,50);
        Page<OrderInfoEntity> list = orderInfoService.selectNotSynZiyunOrderPage(page, startTime);
        // 总页数
        int totalPages = list.getPages();
        do {
            logger.info("同步紫云网订单起---订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+currentPage);
            if(list != null && list.getTotal() > 0){
                logger.info(""+list.getRecords());
                erpOrderService.synProductionOrder(list.getRecords());
            }
            currentPage++;
            if(currentPage <= totalPages){
//                page = new Page<>(currentPage,50);
                page = new Page<>(1,50);
                list = orderInfoService.selectNotSynZiyunOrderPage(page, startTime);
            }
        }while (currentPage < totalPages);

        logger.info("同步紫云网订单同步完成");

    }

    /**
     * 同步 紫云网订单支付信息
     */
    public void synOrderPayInfo(){
        erpOrderService.synPayInfo();
//        logger.info("***********同步紫云网订单支付信息  start  ***************");
//
//        // 当前页数
//        int currentPage = 0;
//        Page<OrderInfoEntity> page = new Page<>(currentPage,50);
//        // 查询所有已同步订单，支付信息变更的订单
//        Page<OrderInfoEntity> list = orderInfoService.selectPayChangeZiyunOrderPage(page);
//        // 总页数
//        int totalPages = list.getPages();
//
//        do{
//            logger.info("订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+(currentPage+1));
//            if(list != null && list.getTotal() > 0){
//                erpOrderService.synOrderPayInfo(list.getRecords());
//            }
//            currentPage++;
//            if(currentPage <= totalPages){
////                page = new Page<>(currentPage,50);
//                page = new Page<>(1,50);
//                list = orderInfoService.selectPayChangeZiyunOrderPage(page);
//            }
//        }while (currentPage < totalPages);
//
//        logger.info("同步紫云网订单同步完成");
    }

    /**
     * 同步 紫云网订单确认收货信息
     */
    public void synOrderReceiveInfo(){
        logger.info("***********同步紫云网订单确认收货信息  start  ***************");

        // 当前页数
        int currentPage = 0;
        Page<OrderInfoEntity> page = new Page<>(currentPage,50);
        // 查询所有已同步订单，支付信息变更的订单
        Page<OrderInfoEntity> list = orderInfoService.selectReceiveChangeZiyunOrderPage(page);
        // 总页数
        int totalPages = list.getPages();

        do{
            logger.info("订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+(currentPage+1));
            if(list != null && list.getTotal() > 0){
                erpOrderService.synOrderReceiveInfo(list.getRecords());
            }
            currentPage++;
            if(currentPage <= totalPages){
                page = new Page<>(currentPage,50);
                list = orderInfoService.selectReceiveChangeZiyunOrderPage(page);
            }
        }while (currentPage < totalPages);

        logger.info("同步紫云网订单确认收货信息完成");
    }

    /**
     * 同步 紫云网取消订单
     */
    public void synOrderCancleInfo(){
        logger.info("***********同步紫紫云网取消订单信息  start  ***************");

        // 当前页数
        int currentPage = 0;
        Page<OrderInfoEntity> page = new Page<>(currentPage,50);
        // 查询所有已同步订单，支付信息变更的订单
        Page<OrderInfoEntity> list = orderInfoService.selectCancleChangeZiyunOrderPage(page);
        // 总页数
        int totalPages = list.getPages();

        do{
            logger.info("订单数量：" +list.getTotal()+"---每页订单数：50---总页数："+totalPages+"---当前页数："+(currentPage+1));
            if(list != null && list.getTotal() > 0){
                erpOrderService.synOrderCancleInfo(list.getRecords());
            }
            currentPage++;
            if(currentPage <= totalPages){
                page = new Page<>(currentPage,50);
                list = orderInfoService.selectCancleChangeZiyunOrderPage(page);
            }
        }while (currentPage < totalPages);

        logger.info("同步紫云网取消订单信息完成");
    }

}
